<template>
  <div>
    <div class="wp1080 cf">
      <el-col :span="24">
        <rootPath-view :child-msg="current"></rootPath-view>
      </el-col>
      <el-col :span="16" class="right-border">
        <div class="left-header">
          <div class="title">
            <div>搜索{{searchWord}}的最新应用结果</div>
            <span @click="type=true" :class="[{ bjBlue: type }]">最新</span>
            <span @click="type=false" :class="[{ bjBlue: !type }]">热门</span>
          </div>
        </div>
        <div class="leftList">
          <el-col :span="8" v-for="item in dataList" :key="item.id">
            <leftList-view v-if="type" :child-msg="item"></leftList-view>
          </el-col>
          <el-col :span="8" v-for="item in dataList" :key="item.id">
            <leftList-view v-if="!type" :child-msg="item"></leftList-view>
          </el-col>
        </div>
      </el-col>
      <el-col :span="7" :offset="1">
        <div class="right-border" style="height: 884px;overflow: hidden;box-sizing: border-box;">
          <div class="right-title">热门推荐</div>
          <el-col :span="24" v-for="item in dataList" :key="item.id">
            <introduceList-view @toDetail="toDetail" :child-msg="item"></introduceList-view>
          </el-col>
        </div>
      </el-col>
    </div>
    <easyfooter></easyfooter>
  </div>
</template>

<script>
import leftList from '../components/leftList.vue'
import introduceList from '../components/introduceList.vue'
import rootPath from '../components/rootPath.vue'
import easyfooter from '../components/easy-footer.vue'

export default {
  name: 'search',
  components: {
    'leftList-view': leftList,
    'introduceList-view': introduceList,
    'rootPath-view': rootPath,
    'easyfooter': easyfooter
  },
  data () {
    return {
      type: true,
      searchWord: '',
      current: '搜索结果',
      dataList: []
    }
  },
  watch: {
    '$route':'routeChange'
  },
  methods: {
    toDetail (id) {
      this.$router.push({path: '/detail', query: {userId: id}})
    },
    routeChange(){
      let that = this
      let searchAPI = '/pc/search'
      this.searchWord = this.$route.query.text
      // 此地有传auth(token)认证 为空就不会执行
      this.$request.post(searchAPI, {'keyWord': this.searchWord, 'page': '1'}, true,
        function (res) {
          if (res.data.code === 1000) {
            that.dataList = res.data.rst
          }
        }
      )
    }
  },
  created () {
    let searchAPI = '/pc/search'
    let that = this
    this.searchWord = this.$route.query.text
    // 此地有传auth(token)认证 为空就不会执行
    this.$request.post(searchAPI, {'keyWord': this.searchWord, 'page': '1'}, true,
      function (res) {
        if (res.data.code === 1000) {
          that.dataList = res.data.rst
        }
      }
    )
    // this.$nextTick(() => {
    //   this.searchWord = this.$route.query.text
    //   // 此地有传auth(token)认证 为空就不会执行
    //   this.$request.post(searchAPI, {'keyWord': this.searchWord, 'page': '1'}, true,
    //     function (res) {
    //       if (res.data.code === 1000) {
    //         that.dataList = res.data.rst
    //       }
    //     }
    //   )
    // })
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      // 通过 `vm` 访问组件实例
      // console.log(vm)
    })
    // 在渲染该组件的对应路由被 confirm 前调用
    // 不！能！获取组件实例 `this`
    // 因为当守卫执行前，组件实例还没被创建
  }
}
</script>
<style scoped>
  .left-header .title{
    height: 30px;
    line-height: 30px;
    width: 100%;
    padding: 0 0 15px 0;
  }
  .left-header div{
    float: left;
    color: rgba(115, 115, 115, 1);
    font-size: 20px;
    text-align: left;
    font-family: Roboto;
  }
  .left-header span{
    cursor: pointer;
    float: right;
    color: rgba(115, 115, 115, 1);
    font-size: 14px;
    text-align: right;
    font-family: Roboto;
    padding: 0 5px;
  }
  .leftList{
    clear: both;
  }
  .bjBlue{
    color: #009688 !important;
  }

</style>
